package service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import dao.IOxfDao;
import entities.TOxf;
import interfaces.IOxfService;
import util.ParamUtil;
import utils.StringUtil;
import views.VOperatorFunction;

@Service
public class OxfService implements IOxfService {

	@Autowired
	private IOxfDao oxfDao;

	public TOxf persist(TOxf entityBean) throws Exception {
		if (StringUtil.isEmpty(entityBean)) {
			return null;
		}
		if (StringUtil.isEmpty(entityBean.getOptNo()) || StringUtil.isEmpty(entityBean.getFunNo())) {
			return null;
		}
		oxfDao.insert(entityBean);
		return entityBean;
	}

	public Integer remove(TOxf entityBean) throws Exception {
		if (StringUtil.isEmpty(entityBean)) {
			return 0;
		}
		if (StringUtil.isEmpty(entityBean.getOptNo()) && StringUtil.isEmpty(entityBean.getFunNo())) {
			return 0;
		}
		return oxfDao.delete(entityBean);
	}

	public Integer expunge(Map<String, Object> paramMap) throws Exception {
		return oxfDao.clear(paramMap);
	}

	public Integer record(Map<String, Object> paramMap) throws Exception {
		return oxfDao.count(paramMap);
	}

	public ResultSet<TOxf> search(QuerySet<Object> querySet) throws Exception {
		ResultSet<TOxf> resultSet = new ResultSet<>();
		if (StringUtil.isEmpty(querySet)) {
			return resultSet;
		}
		PageInfo pageInfo = querySet.getPageInfo();
		SortInfo sortInfo = querySet.getSortInfo();
		Map<String, Object> paramMap = querySet.getParamMap();
		if (StringUtil.isEmpty(pageInfo)) {
			pageInfo = new PageInfo();
		}
		if (!StringUtil.isEmpty(sortInfo)) {
			paramMap = ParamUtil.sortMap(paramMap, sortInfo);
		}
		PageHelper.startPage(pageInfo.getPageIndex(), pageInfo.getPageSize(), false);
		List<TOxf> resultList = oxfDao.query(paramMap);
		pageInfo.setPageRecord(oxfDao.count(querySet.getParamMap()));
		pageInfo.allocation();
		resultSet.setResultList(resultList);
		resultSet.setPageInfo(pageInfo);
		return resultSet;
	}

	public ResultSet<VOperatorFunction> relate(QuerySet<Object> querySet) {
		ResultSet<VOperatorFunction> resultSet = new ResultSet<>();
		if (StringUtil.isEmpty(querySet)) {
			return resultSet;
		}
		PageInfo pageInfo = querySet.getPageInfo();
		SortInfo sortInfo = querySet.getSortInfo();
		Map<String, Object> paramMap = querySet.getParamMap();
		if (StringUtil.isEmpty(pageInfo)) {
			pageInfo = new PageInfo();
		}
		if (!StringUtil.isEmpty(sortInfo)) {
			paramMap = ParamUtil.sortMap(paramMap, sortInfo);
		}
		PageHelper.startPage(pageInfo.getPageIndex(), pageInfo.getPageSize(), false);
		List<VOperatorFunction> resultList = oxfDao.extend(paramMap);
		pageInfo.setPageRecord(oxfDao.gross(querySet.getParamMap()));
		pageInfo.allocation();
		resultSet.setResultList(resultList);
		resultSet.setPageInfo(pageInfo);
		return resultSet;
	}

}
